package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.BatteryConstant;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.Battery;
import com.tbit.main.pojo.Machine;
import com.tbit.main.pojo.MachineLose;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 丢失车辆
 *
 * @author LMY
 * @create 2021-05-11 16:42
 */

@RestController
@RequestMapping("/machineLost")
public class MachineLostController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private MachineLoseService machineLoseService;
    @Autowired
    private BatteryService batteryService;
    @Autowired
    private BatteryLogService batteryLogService;

    /**
     * 插入
     *
     * @param token
     * @param machineLose
     * @param batteryNO
     * @return
     */
    @RequestMapping("/insert")
    @OperateLog(module = ModuleEnum.MACHINELOST, methods = MethodEnum.MACHINELOST_ADD)
    public JsonResult insert(String token, MachineLose machineLose, String batteryNO) {
        Assert.hasLength(machineLose.getUserCode(), "Machine.userCode.notNull");
        Assert.hasLength(machineLose.getReason(), "AccountEvent.content.notNull");
        Assert.notNull(machineLose.getAccountId(), "AccountUser.agentId.notNull");

        AccountUser accountUserToken = tokenService.getAccount(token);

        /**查询设备信息*/
        Machine machine = machineService.getByUseCode(machineLose.getUserCode());
        if (machine == null) {
            throw new BaseException("Machine.notExist");
        }

        /**设备权限判断*/
        machineService.auth(accountUserToken.getAgents(), machine.getMachineId());

        MachineLose machineLoseDB = machineLoseService.getAccountIdAndUserCode(machineLose.getAccountId(), machineLose.getUserCode());
        if (machineLoseDB == null) {
            machineLose.setAddMan(accountUserToken.getName());
            machineLose.setMachineId(machine.getMachineId());
            machineLose.setMachineNO(machine.getMachineNO());
            machineLoseService.insert(machineLose);
        } else {
            throw new BaseException("Machine.userCode.exist");
        }

        if (batteryNO != null) {
            batteryService.updateIdState(batteryNO, null, accountUserToken.getAccountUserId(), BatteryConstant.Battery_State_DS, machineLose.getRemark());
            batteryLogService.insert(batteryNO, accountUserToken.getAccountUserId(), BatteryConstant.Battery_Type_DS, null);
        } else {
            Battery battery = batteryService.getByMachineId(machine.getMachineId());
            if (battery != null) {
                batteryService.updateIdState(battery.getBatteryNO(), machine.getUserCode(), accountUserToken.getAccountUserId(), null, machineLose.getRemark());
            }
        }

        return JsonResult.succ();
    }

    /**
     * 删除
     *
     * @param token
     * @param userCode
     * @param accountId
     * @param batteryNO
     * @return
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.MACHINELOST, methods = MethodEnum.MACHINELOST_DEL)
    public JsonResult insert(String token, String userCode, Integer accountId, String batteryNO) {
        Assert.hasLength(userCode, "Machine.userCode.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        AccountUser accountUserToken = tokenService.getAccount(token);

        /**查询设备信息*/
        Machine machine = machineService.getByUseCode(userCode);
        if (machine == null) {
            throw new BaseException("Machine.notExist");
        }

        /**设备权限判断*/
        machineService.auth(accountUserToken.getAgents(), machine.getMachineId());

        MachineLose machineLoseDB = machineLoseService.getAccountIdAndUserCode(accountId, userCode);
        if (machineLoseDB == null) {
            throw new BaseException("Machine.notExist");
        } else {
            machineLoseService.del(userCode);
        }

        if (batteryNO != null) {
            batteryService.updateIdState(batteryNO, null, accountUserToken.getAccountUserId(), BatteryConstant.Battery_State_KGZ, null);
            batteryLogService.insert(batteryNO, accountUserToken.getAccountUserId(), BatteryConstant.Battery_Type_XH, null);
        } else {
            Battery battery = batteryService.getByMachineId(machine.getMachineId());
            if (battery != null) {
                batteryService.updateIdState(battery.getBatteryNO(), machine.getUserCode(), accountUserToken.getAccountUserId(), null, null);
            }
        }


        return JsonResult.succ();
    }

    /**
     * 根据车辆编号查询
     *
     * @param token
     * @param userCode
     * @param accountId
     * @return
     */
    @RequestMapping("/getByUserCode")
    public JsonResult getByUserCode(String token, String userCode, Integer accountId) {
        Assert.hasLength(userCode, "Machine.userCode.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        AccountUser accountUserToken = tokenService.getAccount(token);

        /**查询设备信息*/
        Machine machine = machineService.getByUseCode(userCode);
        if (machine == null) {
            throw new BaseException("Machine.notExist");
        }

        /**设备权限判断*/
        machineService.auth(accountUserToken.getAgents(), machine.getMachineId());

        MachineLose machineLoseDB = machineLoseService.getAccountIdAndUserCode(accountId, userCode);


        if (machineLoseDB == null) {
            throw new BaseException("Machine.notExist");
        } else {
            Battery battery = batteryService.getByMachineId(machine.getMachineId());
            if (battery != null) {
                machineLoseDB.setBatteryNO(battery.getBatteryNO());
            }
            return JsonResult.succ(machineLoseDB);
        }
    }

    /**
     * 根据区域查询
     *
     * @param token
     * @param accountId
     * @param rowCount
     * @param pageNO
     * @return
     */
    @RequestMapping("/getByAccountId")
    public JsonResult getByAccountId(String token, Integer accountId, Integer rowCount, Integer pageNO) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        if (rowCount == null) {
            rowCount = 10;
        }

        if (pageNO == null) {
            pageNO = 1;
        }

        Integer accountCount = machineLoseService.getByAccountIdCount(accountId);
        List<MachineLose> machineLoses = new LinkedList<>();
        if (accountCount > 0) {
            machineLoses = machineLoseService.getByAccountId(accountId, pageNO, rowCount);
        }

        if (machineLoses != null && machineLoses.size() > 0) {
            List<Integer> machineIds = machineLoses.stream().map(MachineLose::getMachineId).collect(Collectors.toList());
            List<Battery> batteries = batteryService.getByMachineIds(machineIds);
            if (batteries != null && batteries.size() > 0) {

            }
            Map<Integer, String> map = new HashMap<>();
            for (Battery battery : batteries) {
                map.put(battery.getMachineId(), battery.getBatteryNO());
            }
            for (MachineLose machineLose : machineLoses) {
                machineLose.setBatteryNO(map.get(machineLose.getMachineId()));
            }
        }


        return JsonResult.succ(machineLoses);
    }
}


